package com.naihe.anderson.sms.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.naihe.anderson.sms.dbhelper.DbReadWriteLock;
import com.naihe.anderson.sms.dbhelper.NhSmsDBHelper;
import com.naihe.anderson.sms.entity.Nhsms;
import com.naihe.anderson.sms.entity.NhsmsSession;
import com.naihe.anderson.sms.util.FileUtil;

public class NhSmsDao {
	private NhSmsDBHelper dbHelper;
	private SQLiteDatabase db;
	private static DbReadWriteLock dbReadWriteLock = new DbReadWriteLock();

	public NhSmsDao(Context context) {
		dbHelper = new NhSmsDBHelper(context);
		db = dbHelper.getWritableDatabase();
	}

	public void close() {
		if (dbHelper != null) {
			dbHelper.close();
		}
	}	
	
	public void clearDb(){
		try {
			dbReadWriteLock.writeLock();
			db.beginTransaction();
			db.delete(NhSmsDBHelper.NHSMS_TABLE_NAME, null, null);
			db.delete(NhSmsDBHelper.NHSMS_SESSION_TABLE_NAME, null, null);
			db.setTransactionSuccessful();
		} catch (Exception e) {
			FileUtil.addLog("NhSmsDao::deleteNhSmsEntity", NhSmsDao.class, "NhSmsDao", e);
		} finally {
			db.endTransaction();
			dbReadWriteLock.writeUnlock();
		}
	}
	
	/**
	 * 删除会话中某几条短信数据
	 * @param session
	 * @param list
	 */
	public void deleteNhSmsEntity(NhsmsSession nhsmsSession, List<Nhsms> list){
		try {
			dbReadWriteLock.writeLock();
			db.beginTransaction();
			 for(Nhsms nhsms : list){
				 db.delete(NhSmsDBHelper.NHSMS_TABLE_NAME, Nhsms._ID + " = ? ", new String[]{nhsms.get_id() + ""});
			 }
			 
			 /* do update the session entity*/
			 ContentValues values = new ContentValues();
			 values.put(NhsmsSession._TID, nhsmsSession.getTid());
			 values.put(NhsmsSession.ADDRESS, nhsmsSession.getAddress());
			 values.put(NhsmsSession.BODY, nhsmsSession.getBody());
			 values.put(NhsmsSession.DATE, nhsmsSession.getDate());
			 values.put(NhsmsSession.PERSON, nhsmsSession.getPerson());
			 values.put(NhsmsSession.PROTOCOL, nhsmsSession.getProtocol());
			 values.put(NhsmsSession.READ, nhsmsSession.getRead());
			 values.put(NhsmsSession.NUM_SIZE, nhsmsSession.getNumsize());
			 db.replace(NhSmsDBHelper.NHSMS_SESSION_TABLE_NAME, null, values);
			 values.clear();
			 
			 db.setTransactionSuccessful();
		} catch (Exception e) {
			FileUtil.addLog("NhSmsDao::deleteNhSmsEntity", NhSmsDao.class, "NhSmsDao", e);
		} finally {
			db.endTransaction();
			dbReadWriteLock.writeUnlock();
		}
	}
	
	/**
	 * 
	 * @param list
	 */
	public void deleteNhSmsSessionEntity(List<NhsmsSession> list){
		try {
			dbReadWriteLock.writeLock();
			db.beginTransaction();
			 for(NhsmsSession nhsmsSession : list){
				 db.delete(NhSmsDBHelper.NHSMS_SESSION_TABLE_NAME, NhsmsSession._TID + " = ? ", new String[]{nhsmsSession.getTid() + ""});
				 db.delete(NhSmsDBHelper.NHSMS_TABLE_NAME, Nhsms.THREAD_ID + " = ? ", new String[]{nhsmsSession.getTid() + ""});
			 }
			db.setTransactionSuccessful();
		} catch (Exception e) {
			FileUtil.addLog("NhSmsDao::deleteNhSmsSessionEntity", NhSmsDao.class, "NhSmsDao", e);
		} finally {
			db.endTransaction();
			dbReadWriteLock.writeUnlock();
		}
	}
	
	public void updateReadType(NhsmsSession nhsmsSession){
		try {
			 dbReadWriteLock.writeLock();
			 db.beginTransaction();
			 
			 String sql="update " + NhSmsDBHelper.NHSMS_SESSION_TABLE_NAME + " set " + NhsmsSession.READ + "=1 where " + NhsmsSession._TID + "=" + nhsmsSession.getTid();
			 db.execSQL(sql);
			 sql="update " + NhSmsDBHelper.NHSMS_TABLE_NAME + " set " + Nhsms.READ + "=1 where " + Nhsms.THREAD_ID + "=" + nhsmsSession.getTid();
			 db.execSQL(sql);
			 
			 db.setTransactionSuccessful();
		}catch (Exception e) {
			FileUtil.addLog("NhSmsDao::updateReadType", NhSmsDao.class, "NhSmsDao", e);
		}finally{
			db.endTransaction();
			dbReadWriteLock.writeUnlock();
		}
	}
	
	public void setSessionPasswd(long sessionId, String passwd){
		try {
			 dbReadWriteLock.writeLock();
			 db.beginTransaction();
			 
			 String sql="update " + NhSmsDBHelper.NHSMS_SESSION_TABLE_NAME + " set " + NhsmsSession.CIPHER + "='" + passwd + "' where " + NhsmsSession._TID + "=" + sessionId;
			 db.execSQL(sql);
			 
			 db.setTransactionSuccessful();
		}catch (Exception e) {
			FileUtil.addLog("NhSmsDao::setSessionPasswd", NhSmsDao.class, "NhSmsDao", e);
		}finally{
			db.endTransaction();
			dbReadWriteLock.writeUnlock();
		}
	}
	/**
	 * 
	 * @param list
	 */
	public void insertOrUpdateNhSmsSessionEntity(List<NhsmsSession> list){
		try {
			 dbReadWriteLock.writeLock();
			 db.beginTransaction();
			 ContentValues values = new ContentValues();
			 for(NhsmsSession nhsmsSession : list){
				 values.put(NhsmsSession._TID, nhsmsSession.getTid());
				 values.put(NhsmsSession.ADDRESS, nhsmsSession.getAddress());
				 values.put(NhsmsSession.BODY, nhsmsSession.getBody());
				 values.put(NhsmsSession.DATE, nhsmsSession.getDate());
				 values.put(NhsmsSession.PERSON, nhsmsSession.getPerson());
				 values.put(NhsmsSession.PROTOCOL, nhsmsSession.getProtocol());
				 values.put(NhsmsSession.READ, nhsmsSession.getRead());
				 values.put(NhsmsSession.NUM_SIZE, nhsmsSession.getNumsize());
				 values.put(NhsmsSession.CIPHER, nhsmsSession.getCipher());
				 
				 db.replace(NhSmsDBHelper.NHSMS_SESSION_TABLE_NAME, null, values);
				 values.clear();
			 }
			 values = null;
			 db.setTransactionSuccessful();
		}catch (Exception e) {
			FileUtil.addLog("NhSmsDao::insertOrUpdateNhSmsSessionEntity", NhSmsDao.class, "NhSmsDao", e);
		}finally{
			db.endTransaction();
			dbReadWriteLock.writeUnlock();
		}
	}
	
	/**
	 * 
	 * @param list
	 */
	public void insertOrUpdateNhSmsEntity(List<Nhsms> list){
		try {
			 dbReadWriteLock.readLock();
			 db.beginTransaction();
			 ContentValues values = new ContentValues();
			 for(Nhsms nhsms : list){
				 values.put(Nhsms._ID, nhsms.get_id());
				 values.put(Nhsms.ADDRESS, nhsms.getAddress());
				 values.put(Nhsms.BODY, nhsms.getBody());
				 values.put(Nhsms.DATE, nhsms.getDate());
				 values.put(Nhsms.DATE_SENT, nhsms.getDate_sent());
				 values.put(Nhsms.ERROR_CODE, nhsms.getError_code());
				 values.put(Nhsms.LOCKED, nhsms.getLocked());
				 values.put(Nhsms.PERSON, nhsms.getPerson());
				 values.put(Nhsms.PROTOCOL, nhsms.getProtocol());
				 values.put(Nhsms.READ, nhsms.getRead());
				 values.put(Nhsms.REPLY_PATH_PRESENT, nhsms.getReply_path_present());
				 values.put(Nhsms.SEEN, nhsms.getSeen());
				 values.put(Nhsms.SERVICE_CENTER, nhsms.getService_center());
				 values.put(Nhsms.STATUS, nhsms.getStatus());
				 values.put(Nhsms.SUBJECT, nhsms.getSubject());
				 values.put(Nhsms.THREAD_ID, nhsms.getThread_id());
				 values.put(Nhsms.TYPE, nhsms.getType());
				 
				 db.replace(NhSmsDBHelper.NHSMS_TABLE_NAME, null, values);
				 values.clear();
			 }
			 values = null;
			 db.setTransactionSuccessful();
		}catch (Exception e) {
			FileUtil.addLog("NhSmsDao::insertOrUpdateNhSmsEntity", NhSmsDao.class, "NhSmsDao", e);
		}finally{
			db.endTransaction();
			dbReadWriteLock.readUnlock();
		}
	}
	
	/**
	 * 获取短信数量
	 * @return
	 */
	public int getNhsmsCount(){
		int tCount = -1;
		Cursor cthread_id = null;
		try {
			dbReadWriteLock.readLock();
			String sql="select count(" + Nhsms._ID + ") from " + NhSmsDBHelper.NHSMS_TABLE_NAME + ";";
			cthread_id = db.rawQuery(sql, null);
			
			if (cthread_id.moveToFirst()) {
				tCount = cthread_id.getInt(0);
			}else{
				tCount = -1;
			}
		} catch (Exception e) {
			FileUtil.addLog("NhSmsDao::getNhsmsCount", NhSmsDao.class, "NhSmsDao", e);
		} finally{
			if (cthread_id != null) { cthread_id.close(); }
			dbReadWriteLock.readUnlock();
		}	
		FileUtil.addLog("NhSmsDao::getNhsmsCount tCount="+tCount, NhSmsDao.class, "NhSmsDao", null);
		return tCount;
	}
	
	/**
	 * 获取短信会话数量
	 * @return
	 */
	public int getNhsmsSessionCount(){
		int tCount = -1;
		Cursor cthread_id = null;
		try {
			dbReadWriteLock.readLock();
			String sql="select count(" + NhsmsSession._TID + ") from " + NhSmsDBHelper.NHSMS_SESSION_TABLE_NAME + ";";
			cthread_id = db.rawQuery(sql, null);
			
			if (cthread_id.moveToFirst()) {
				tCount = cthread_id.getInt(0);
			}else{
				tCount = -1;
			}
		} catch (Exception e) {
			FileUtil.addLog("NhSmsDao::getNhsmsSessionCount", NhSmsDao.class, "NhSmsDao", e);
		} finally{
			if (cthread_id != null) { cthread_id.close(); }
			dbReadWriteLock.readUnlock();
		}	
		FileUtil.addLog("NhSmsDao::getNhsmsSessionCount tCount="+tCount, NhSmsDao.class, "NhSmsDao", null);
		return tCount;
	}
	/**
	 * 
	 * @param sessionTid
	 * @return
	 */
	public List<Nhsms> queryAllNhsmsList(String sessionTid, int pageNumber){
		List<Nhsms> list = new ArrayList<Nhsms>();
		Cursor smsCursor = null;
		try {
			dbReadWriteLock.readLock();
			smsCursor = db.query(NhSmsDBHelper.NHSMS_TABLE_NAME,NhSmsDBHelper.nhsms_projection, Nhsms.THREAD_ID + "=?",
					 new String[]{sessionTid}, null, null, Nhsms.DATE + " desc", "" + NhsmsSession.PAGE_SIZE * pageNumber);
		
			if(smsCursor != null && smsCursor.moveToFirst()){
				for (int j = 0; j < smsCursor.getCount(); j++) {
					Nhsms nhsms = new Nhsms();
					nhsms.set_id(smsCursor.getLong(smsCursor.getColumnIndex(Nhsms._ID)));
					nhsms.setAddress(smsCursor.getString(smsCursor.getColumnIndex(Nhsms.ADDRESS)));
					nhsms.setBody(smsCursor.getString(smsCursor.getColumnIndex(Nhsms.BODY)));
					nhsms.setDate(smsCursor.getLong(smsCursor.getColumnIndex(Nhsms.DATE)));
					nhsms.setDate_sent(smsCursor.getLong(smsCursor.getColumnIndex(Nhsms.DATE_SENT)));
					nhsms.setError_code(smsCursor.getInt(smsCursor.getColumnIndex(Nhsms.ERROR_CODE)));
					nhsms.setLocked(smsCursor.getInt(smsCursor.getColumnIndex(Nhsms.LOCKED)));
					nhsms.setPerson(smsCursor.getLong(smsCursor.getColumnIndex(Nhsms.PERSON)));
					nhsms.setProtocol(smsCursor.getInt(smsCursor.getColumnIndex(Nhsms.PROTOCOL)));
					nhsms.setRead(smsCursor.getInt(smsCursor.getColumnIndex(Nhsms.READ)));
					nhsms.setReply_path_present(smsCursor.getInt(smsCursor.getColumnIndex(Nhsms.REPLY_PATH_PRESENT)));
					nhsms.setSeen(smsCursor.getInt(smsCursor.getColumnIndex(Nhsms.SEEN)));
					nhsms.setService_center(smsCursor.getString(smsCursor.getColumnIndex(Nhsms.SERVICE_CENTER)));
					nhsms.setStatus(smsCursor.getInt(smsCursor.getColumnIndex(Nhsms.STATUS)));
					nhsms.setSubject(smsCursor.getString(smsCursor.getColumnIndex(Nhsms.SUBJECT)));
					nhsms.setThread_id(smsCursor.getLong(smsCursor.getColumnIndex(Nhsms.THREAD_ID)));
					nhsms.setType(smsCursor.getInt(smsCursor.getColumnIndex(Nhsms.TYPE)));
					
					list.add(nhsms);
					smsCursor.moveToNext();
				}
			}
		} catch (Exception e) {
			FileUtil.addLog("NhSmsDao::queryAllNhsmsList", NhSmsDao.class, "NhSmsDao", e);
		} finally{
			if (smsCursor != null) { smsCursor.close(); }
			dbReadWriteLock.readUnlock();
		}
		Collections.sort(list);
		return list;
	}
	
	public List<Nhsms> searchNhsmsList(String searchKey){
		List<Nhsms> list = new ArrayList<Nhsms>();
		Cursor smsCursor = null;
		try {
			dbReadWriteLock.readLock();
			smsCursor = db.query(NhSmsDBHelper.NHSMS_TABLE_NAME,NhSmsDBHelper.nhsms_projection, Nhsms.BODY + " like '%" +searchKey+ "%'",
					 null, null, null, null);
		
			if(smsCursor != null && smsCursor.moveToFirst()){
				for (int j = 0; j < smsCursor.getCount(); j++) {
					Nhsms nhsms = new Nhsms();
					nhsms.set_id(smsCursor.getLong(smsCursor.getColumnIndex(Nhsms._ID)));
					nhsms.setAddress(smsCursor.getString(smsCursor.getColumnIndex(Nhsms.ADDRESS)));
					nhsms.setBody(smsCursor.getString(smsCursor.getColumnIndex(Nhsms.BODY)));
					nhsms.setDate(smsCursor.getLong(smsCursor.getColumnIndex(Nhsms.DATE)));
					nhsms.setDate_sent(smsCursor.getLong(smsCursor.getColumnIndex(Nhsms.DATE_SENT)));
					nhsms.setError_code(smsCursor.getInt(smsCursor.getColumnIndex(Nhsms.ERROR_CODE)));
					nhsms.setLocked(smsCursor.getInt(smsCursor.getColumnIndex(Nhsms.LOCKED)));
					nhsms.setPerson(smsCursor.getLong(smsCursor.getColumnIndex(Nhsms.PERSON)));
					nhsms.setProtocol(smsCursor.getInt(smsCursor.getColumnIndex(Nhsms.PROTOCOL)));
					nhsms.setRead(smsCursor.getInt(smsCursor.getColumnIndex(Nhsms.READ)));
					nhsms.setReply_path_present(smsCursor.getInt(smsCursor.getColumnIndex(Nhsms.REPLY_PATH_PRESENT)));
					nhsms.setSeen(smsCursor.getInt(smsCursor.getColumnIndex(Nhsms.SEEN)));
					nhsms.setService_center(smsCursor.getString(smsCursor.getColumnIndex(Nhsms.SERVICE_CENTER)));
					nhsms.setStatus(smsCursor.getInt(smsCursor.getColumnIndex(Nhsms.STATUS)));
					nhsms.setSubject(smsCursor.getString(smsCursor.getColumnIndex(Nhsms.SUBJECT)));
					nhsms.setThread_id(smsCursor.getLong(smsCursor.getColumnIndex(Nhsms.THREAD_ID)));
					nhsms.setType(smsCursor.getInt(smsCursor.getColumnIndex(Nhsms.TYPE)));
					
					list.add(nhsms);
					smsCursor.moveToNext();
				}
			}
		} catch (Exception e) {
			FileUtil.addLog("NhSmsDao::searchNhsmsList", NhSmsDao.class, "NhSmsDao", e);
		} finally{
			if (smsCursor != null) { smsCursor.close(); }
			dbReadWriteLock.readUnlock();
		}
		return list;
	}
	/**
	 * 
	 * @return
	 */
	public List<NhsmsSession> queryAllNhsmsSessionList() {
		 List<NhsmsSession> list = new ArrayList<NhsmsSession>();
		 
		 Cursor smscur = null;
		 try {
			 dbReadWriteLock.readLock();
			 smscur = db.query(NhSmsDBHelper.NHSMS_SESSION_TABLE_NAME, NhSmsDBHelper.nhsms_session_projection, null, null, null, null, NhsmsSession.DATE + " desc");
			 if (smscur != null && smscur.moveToFirst()) {
				for (int i = 0; i < smscur.getCount(); i++) {
					NhsmsSession entity = new NhsmsSession();
					entity.setTid(smscur.getLong(smscur.getColumnIndex(NhsmsSession._TID)));
					entity.setAddress(smscur.getString(smscur.getColumnIndex(NhsmsSession.ADDRESS)));
					entity.setBody(smscur.getString(smscur.getColumnIndex(NhsmsSession.BODY)));
					entity.setDate(smscur.getLong(smscur.getColumnIndex(NhsmsSession.DATE)));
					entity.setPerson(smscur.getLong(smscur.getColumnIndex(NhsmsSession.PERSON)));
					entity.setProtocol(smscur.getInt(smscur.getColumnIndex(NhsmsSession.PROTOCOL)));
					entity.setRead(smscur.getInt(smscur.getColumnIndex(NhsmsSession.READ)));
					entity.setNumsize(smscur.getLong(smscur.getColumnIndex(NhsmsSession.NUM_SIZE)));
					entity.setCipher(smscur.getString(smscur.getColumnIndex(NhsmsSession.CIPHER)));
					
					Cursor smsCursor = db.query(NhSmsDBHelper.NHSMS_TABLE_NAME,NhSmsDBHelper.nhsms_projection, Nhsms.THREAD_ID + "=?",
							 new String[]{entity.getTid()+""}, null, null, Nhsms.DATE + " desc", "" + NhsmsSession.PAGE_SIZE);
					List<Nhsms> nhsmsList = new ArrayList<Nhsms>();
					if(smsCursor != null && smsCursor.moveToFirst()){
						for (int j = 0; j < smsCursor.getCount(); j++) {
							Nhsms nhsms = new Nhsms();
							nhsms.set_id(smsCursor.getLong(smsCursor.getColumnIndex(Nhsms._ID)));
							nhsms.setAddress(smsCursor.getString(smsCursor.getColumnIndex(Nhsms.ADDRESS)));
							nhsms.setBody(smsCursor.getString(smsCursor.getColumnIndex(Nhsms.BODY)));
							nhsms.setDate(smsCursor.getLong(smsCursor.getColumnIndex(Nhsms.DATE)));
							nhsms.setDate_sent(smsCursor.getLong(smsCursor.getColumnIndex(Nhsms.DATE_SENT)));
							nhsms.setError_code(smsCursor.getInt(smsCursor.getColumnIndex(Nhsms.ERROR_CODE)));
							nhsms.setLocked(smsCursor.getInt(smsCursor.getColumnIndex(Nhsms.LOCKED)));
							nhsms.setPerson(smsCursor.getLong(smsCursor.getColumnIndex(Nhsms.PERSON)));
							nhsms.setProtocol(smsCursor.getInt(smsCursor.getColumnIndex(Nhsms.PROTOCOL)));
							nhsms.setRead(smsCursor.getInt(smsCursor.getColumnIndex(Nhsms.READ)));
							nhsms.setReply_path_present(smsCursor.getInt(smsCursor.getColumnIndex(Nhsms.REPLY_PATH_PRESENT)));
							nhsms.setSeen(smsCursor.getInt(smsCursor.getColumnIndex(Nhsms.SEEN)));
							nhsms.setService_center(smsCursor.getString(smsCursor.getColumnIndex(Nhsms.SERVICE_CENTER)));
							nhsms.setStatus(smsCursor.getInt(smsCursor.getColumnIndex(Nhsms.STATUS)));
							nhsms.setSubject(smsCursor.getString(smsCursor.getColumnIndex(Nhsms.SUBJECT)));
							nhsms.setThread_id(smsCursor.getLong(smsCursor.getColumnIndex(Nhsms.THREAD_ID)));
							nhsms.setType(smsCursor.getInt(smsCursor.getColumnIndex(Nhsms.TYPE)));
							
							nhsmsList.add(nhsms);
							smsCursor.moveToNext();
						}
					}
					if (smsCursor != null) { smsCursor.close(); }
					Collections.sort(nhsmsList);
					entity.setNhsms(nhsmsList);
					nhsmsList.clear();
					nhsmsList = null;
					
					list.add(entity);
					smscur.moveToNext();
				}
			 }
		 } catch (Exception e) {
			 FileUtil.addLog("NhSmsDao::queryAllNhsmsSessionList", NhSmsDao.class, "NhSmsDao", e);
		 } finally{
			 if (smscur != null) { smscur.close(); }
			 dbReadWriteLock.readUnlock();
		 }
		 
		 return list;
	 }
	
	public Map<Long, String> queryNhsmsSessionCipherList() {
		Map<Long, String> nhsmsSessionCipherMap = new HashMap<Long, String>();
		 
		 Cursor smscur = null;
		 try {
			 dbReadWriteLock.readLock();
			 smscur = db.query(NhSmsDBHelper.NHSMS_SESSION_TABLE_NAME, NhSmsDBHelper.nhsms_session_projection, null, null, null, null, NhsmsSession.DATE + " desc");
			 if (smscur != null && smscur.moveToFirst()) {
				for (int i = 0; i < smscur.getCount(); i++) {
					Long tid = smscur.getLong(smscur.getColumnIndex(NhsmsSession._TID));
					String cipher = smscur.getString(smscur.getColumnIndex(NhsmsSession.CIPHER));
					
					nhsmsSessionCipherMap.put(tid, cipher);
					smscur.moveToNext();
				}
			 }
		 } catch (Exception e) {
			 FileUtil.addLog("NhSmsDao::queryNhsmsSessionCipherList", NhSmsDao.class, "NhSmsDao", e);
		 } finally{
			 if (smscur != null) { smscur.close(); }
			 dbReadWriteLock.readUnlock();
		 }
		 
		 return nhsmsSessionCipherMap;
	 }
	
	/**
	 * 
	 * @return
	 */
	public String queryForSave(List<Long> sessionTidList) {
		StringBuffer nhsmsSave = new StringBuffer();
		try {
			dbReadWriteLock.readLock();
			for(Long sessionTid : sessionTidList){
				Cursor smsCursor = db.query(NhSmsDBHelper.NHSMS_TABLE_NAME,NhSmsDBHelper.nhsms_projection, Nhsms.THREAD_ID + "=?",
						 new String[]{sessionTid.longValue() + ""}, null, null, Nhsms.DATE + " desc");
			
				if(smsCursor != null && smsCursor.moveToFirst()){
					for (int j = 0; j < smsCursor.getCount(); j++) {
						nhsmsSave.append("time:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(smsCursor.getLong(smsCursor.getColumnIndex(Nhsms.DATE))))).append("\r\n");
						nhsmsSave.append("address:" + smsCursor.getString(smsCursor.getColumnIndex(Nhsms.ADDRESS)));
						nhsmsSave.append("type:" + smsCursor.getInt(smsCursor.getColumnIndex(Nhsms.TYPE))).append("\r\n");
						nhsmsSave.append("body:" + smsCursor.getString(smsCursor.getColumnIndex(Nhsms.BODY))).append("\r\n");
						nhsmsSave.append("------------").append("\r\n");
						smsCursor.moveToNext();
					}
				}
				if (smsCursor != null) { smsCursor.close(); }
				nhsmsSave.append("\r\n");
			}
		} catch (Exception e) {
			FileUtil.addLog("NhSmsDao::queryAllNhsmsList", NhSmsDao.class, "NhSmsDao", e);
		} finally{
			dbReadWriteLock.readUnlock();
		}
		return nhsmsSave.toString();
	 }
}
